package com.zcl.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.istack.internal.logging.Logger;
import com.zcl.dao.Boat;
import com.zcl.service.BoatService;
import com.zcl.util.GetCurrentUserDetails;

@Controller
public class BoatController {
	@Autowired
	BoatService boatService;
	Logger logger = Logger.getLogger(BoatController.class);
	/**
	 * 处理	/boat/index
	 * @return	船舶信息页面
	 */
	@RequestMapping("/boat")
	public String boatIndex() {
		return "boat";
	}
	
	/**
	 * 事故信息概况查询
	 */
	@ResponseBody
	@RequestMapping("/boat/surveryQuery")
	public List<Map<String, String>> boatQuery(Model model) {
		List<Map<String, String>> boatList = boatService.boatQuery();
		return boatList;
	}
	
	/**
	 * 事故信息概况-操作-详情
	 * @return
	 */
	@RequestMapping("/boatSurveryInfo")
	public String boatSuveryInfo(@RequestParam("boatId") String boatId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("boatId", boatId);
		Boat boat = boatService.boatSuveryInfo(map);
		model.addAttribute("boat", boat);
		System.out.println("事故信息概况-操作-详情:boatId="+boatId);
		return "boat_information";
	}
	
	/**
	 * 事故信息概况-操作-编辑
	 * @return
	 */
	@RequestMapping("/boatSurveryRevise")
	public String boatSuveryRevise(@RequestParam("boatId") String boatId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("boatId", boatId);
		Boat boat = boatService.boatSuveryInfo(map);
		model.addAttribute("boat", boat);
		System.out.println("事故信息概况-操作-编辑:boatId="+boatId);
		return "boat_revise";
	}
	
	/**
	 * 事故信息概况-操作-编辑2--获取某些下拉框值
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/boatSurveryRevise2")
	public Boat boatSuveryRevise2(@RequestParam("boatId") String boatId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("boatId", boatId);
		Boat boat = boatService.boatSuveryInfo(map);
		
//		model.addAttribute("boat", boat);
		System.out.println("事故信息概况-操作-编辑2--获取某些下拉框值:boatId="+boatId);
		return boat;
	}
	
	/**
	 * 事故信息概况-操作-删除
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/boat/delete")
	public void boatDelete(@RequestParam("boatId") String boatId) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("boatId", boatId);
		boatService.boatDelete(map);
		System.out.println("删除");;
	}
	
	/**
	 * 事故添加页面
	 * @return
	 */
	@RequestMapping("/boatAdd")
	public String boatboatAdd() {
		return "boat_add";
	}
	
	/**
	 * 事故信息查询-全文搜索
	 * @param search_input
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/boat/globalQuery")
	public List<Map<String, String>> boatGlobalQuery(@RequestParam("search_input") String search_input){
		Map<String, String> map = new HashMap<String,String>();
		map.put("search_input", search_input);
		List<Map<String, String>> boatList = boatService.boatGlobalQuery(map);
		return boatList;
		
	} 
	
	/**
	 * 事故信息查询-高级搜索
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/boat/advancedQuery")
	public List<Map<String, String>> boatAdvancedQuery(HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		
		map.put("boatId", request.getParameter("boatId"));
		map.put("boatName", request.getParameter("boatName"));
		map.put("boatKind", request.getParameter("boatKind"));
		map.put("boatWriter", request.getParameter("boatWriter"));
		map.put("boatTime", request.getParameter("boatTime"));
		List<Map<String, String>> boatList = boatService.boatAdvancedQuery(map);
		return boatList;
		
	} 
	
	/**
	 * 事故信息录入
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/boat/addBoat")
	public void boatAdd(HttpServletRequest request) throws UnsupportedEncodingException {
		UserDetails userDetails = GetCurrentUserDetails.currentUser();
		String boatId = UUID.randomUUID().toString();
		Map<String, String> map = new HashMap<String,String>();
		map.put("boatId", boatId);
		map.put("accidentId", request.getParameter("accidentId"));
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("boatKind", request.getParameter("boatKind"));
		map.put("boatLoad", request.getParameter("boatLoad"));
		map.put("boatLogin", request.getParameter("boatLogin"));
		map.put("boatLong", request.getParameter("boatLong"));
		map.put("boatName", request.getParameter("boatName"));
		map.put("boatNation", request.getParameter("boatNation"));
		map.put("boatPeople", request.getParameter("boatPeople"));
		map.put("boatWide", request.getParameter("boatWide"));
		map.put("date", request.getParameter("date"));
		map.put("factory", request.getParameter("factory"));
		map.put("deep", request.getParameter("deep"));
		map.put("tonnage", request.getParameter("tonnage"));
		map.put("boatWriter", userDetails.getUsername());
		logger.info("事故信息录入");
		boatService.boatAdd(map);
	}
	
	/**
	 * 事故信息修改
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/boat/reviseBoat")
	public void boatRevise(HttpServletRequest request) throws UnsupportedEncodingException {
		
		Map<String, String> map = new HashMap<String,String>();
		map.put("boatId", request.getParameter("boatId"));
		map.put("accidentId", request.getParameter("accidentId"));
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("boatKind", request.getParameter("boatKind"));
		map.put("boatLoad", request.getParameter("boatLoad"));
		//map.put("boatName", request.getParameter("boatName"));
		map.put("boatLogin", request.getParameter("boatLogin"));
		map.put("boatLong", request.getParameter("boatLong"));
		map.put("boatName", request.getParameter("boatName"));
		map.put("boatNation", request.getParameter("boatNation"));
		map.put("boatPeople", request.getParameter("boatPeople"));
		map.put("boatWide", request.getParameter("boatWide"));
		map.put("date", request.getParameter("date"));
		map.put("deep", request.getParameter("deep"));
		map.put("tonnage", request.getParameter("tonnage"));
		System.out.println("boatRevise");
		boatService.boatRevise(map);
	}
}
